package com.feng.controller;

import com.alibaba.fastjson.JSON;
import com.feng.dto.NewsDto;
import com.feng.dto.ProductDto;
import com.feng.dto.UserDto;
import com.feng.service.impl.NewsServiceImpl;
import com.feng.service.impl.ProductServiceImpl;
import com.feng.service.impl.UserServiceImpl;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;

/**
 * @author: 丰
 * @date: 2023/1/31 16:55
 */
@Component
public class CreateES {

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private ProductServiceImpl productService;

    @Autowired
    private NewsServiceImpl newsService;

    // 添加文档
    @PostConstruct
    private void addDoc() throws IOException {
        // 判断当前索引是否存在
//        GetIndexRequest userIndex = new GetIndexRequest("user_doc");
//        if (!client.indices().exists(userIndex, RequestOptions.DEFAULT)){
            // 不存在，才创建
            modifyUserDoc();
//        }

//
//        GetIndexRequest productIndex = new GetIndexRequest("product_doc");
//        if (!client.indices().exists(productIndex, RequestOptions.DEFAULT)){
            // 不存在，才创建
            modifyProductDoc();
//        }

//        GetIndexRequest newsIndex = new GetIndexRequest("news_doc");
//        if (!client.indices().exists(newsIndex, RequestOptions.DEFAULT)){
            // 不存在，才创建
            modifyNewsDoc();
//        }
    }

    public void modifyUserDoc() throws IOException {
        List<UserDto> userDtoList = userService.selectAll();
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        for (int i = 0; i < userDtoList.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("user_doc")
                            .id(userDtoList.get(i).getId() + "")
                            .source(JSON.toJSONString(userDtoList.get(i)), XContentType.JSON)
            );
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

    public void modifyProductDoc() throws IOException {
        List<ProductDto> productDtos = productService.selectAll();
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        for (int i = 0; i < productDtos.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("product_doc")
                            .id(productDtos.get(i).getId() + "")
                            .source(JSON.toJSONString(productDtos.get(i)), XContentType.JSON)
            );
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

    public void modifyNewsDoc() throws IOException {
        List<NewsDto> newsDtos = newsService.selectAll();
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        for (int i = 0; i < newsDtos.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("news_doc")
                            .id(newsDtos.get(i).getId() + "")
                            .source(JSON.toJSONString(newsDtos.get(i)), XContentType.JSON)
            );
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

}
